package com.nuc.bigdata.app.service.impl;

import com.nuc.bigdata.app.service.PhotoService;
import com.nuc.bigdata.domain.entity.User;
import com.nuc.bigdata.domain.vo.CommentsVO;
import com.nuc.bigdata.domain.vo.PhotoNewsVO;
import com.nuc.bigdata.feign.NewsCommentsServiceFeign;
import com.nuc.bigdata.feign.PhotoServiceFeign;
import com.nuc.bigdata.feign.PictureServiceFeign;
import com.nuc.bigdata.feign.UserServiceFeign;
import com.nuc.bigdata.feign.dto.NewsCommentsDto;
import com.nuc.bigdata.feign.entity.PhotoNewsCommentsFeign;
import com.nuc.bigdata.feign.entity.PhotoNewsFeign;
import com.nuc.bigdata.feign.entity.PhotoNewsPictureFeign;
import com.nuc.bigdata.feign.entity.PhotoShareMenuFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * 图片资讯Service实现类
 * @author JackLuo
 * @date 2019/11/26
 */
@Service
public class PhotoServiceImpl implements PhotoService {

    private final PhotoServiceFeign photoServiceFeign;
    private final PictureServiceFeign pictureServiceFeign;
    private final NewsCommentsServiceFeign newsCommentsServiceFeign;
    private final UserServiceFeign userServiceFeign;

    @Autowired
    PhotoServiceImpl(PhotoServiceFeign photoServiceFeign,
                     PictureServiceFeign pictureServiceFeign,
                     NewsCommentsServiceFeign newsCommentsServiceFeign,
                     UserServiceFeign userServiceFeign) {
        this.photoServiceFeign = photoServiceFeign;
        this.pictureServiceFeign = pictureServiceFeign;
        this.newsCommentsServiceFeign = newsCommentsServiceFeign;
        this.userServiceFeign = userServiceFeign;
    }

    /**
     * 查询所有图片资讯菜单项
     * @return 包含菜单项feign的ArrayList
     */
    @Override
    public ArrayList<PhotoShareMenuFeign> listAllMenu() {
        return photoServiceFeign.listAllMenu();
    }

    /**
     * 根据指定菜单id查询所有的图片资讯VO
     * @param menuId 菜单id
     * @return 包含图片资讯VO的ArrayList
     */
    @Override
    public ArrayList<PhotoNewsVO> listByMenuId(String menuId) {
        ArrayList<PhotoNewsFeign> photoNewsFeigns = photoServiceFeign.listNewsByMenuId(menuId);
        ArrayList<PhotoNewsVO> news = new ArrayList<>();
        for (PhotoNewsFeign newsFeign:photoNewsFeigns){
            PhotoNewsVO photoNewsVO = transformPhotoNewsFeign(newsFeign);
            news.add(photoNewsVO);
        }
        return news;
    }

    /**
     * 查询所有的图片资讯VO
     * @return 包含图片资讯VO的ArrayList
     */
    @Override
    public ArrayList<PhotoNewsVO> listNews() {
        ArrayList<PhotoNewsFeign> photoNewsFeigns = photoServiceFeign.listNews();
        ArrayList<PhotoNewsVO> news = new ArrayList<>();
        for (PhotoNewsFeign newsFeign:photoNewsFeigns){
            PhotoNewsVO photoNewsVO = transformPhotoNewsFeign(newsFeign);
            news.add(photoNewsVO);
        }
        return news;
    }

    /**
     * 通过图片资讯id查询指定的图片资讯VO
     * @param pnId 图片资讯唯一标识
     * @return 图片资讯VO
     */
    @Override
    public PhotoNewsVO queryByPnId(String pnId) {
        PhotoNewsFeign newsFeign = photoServiceFeign.queryNews(pnId);
        return transformPhotoNewsFeign(newsFeign);
    }

    /**
     * 查询指定图片资讯对应的评论
     * @param pnId 图片资讯唯一标识
     * @param pageSize 页码
     * @return 包含评论VO的ArrayList
     */
    @Override
    public ArrayList<CommentsVO> listCommentsByPnId(String pnId, Long pageSize) {
        ArrayList<PhotoNewsCommentsFeign> photoNewsCommentsFeigns = newsCommentsServiceFeign.queryPhotosCommentsFeign(pnId, pageSize);
        ArrayList<CommentsVO> voArrayList = new ArrayList<>();
        for (PhotoNewsCommentsFeign commentsFeign:photoNewsCommentsFeigns){
            CommentsVO commentsVO = transformPhotosComments(commentsFeign);
            voArrayList.add(commentsVO);
        }
        return voArrayList;
    }

    /**
     * 创建评论
     * @param pnId 图片资讯唯一标识
     * @param commentsDto 评论内容Dto，包含userId和content
     */
    @Override
    public void createComment(String pnId, NewsCommentsDto commentsDto) {
        newsCommentsServiceFeign.createPhotosComments(pnId, commentsDto);
    }

    /**
     * 用户点赞评论
     * @param userId 用户唯一标识
     * @param photoCommentId 图片资讯评论文艺标识
     */
    @Override
    public void toCommentsLike(String userId, String photoCommentId) {
        newsCommentsServiceFeign.toPhotoCommentLike(photoCommentId,userId );
    }


    /**
     * 将评论feign转化为需要展示的评论VO
     * @param commentsFeign 评论服务返回的评论feign
     * @return 评论VO
     */
    private CommentsVO transformPhotosComments(PhotoNewsCommentsFeign commentsFeign){
        User user = userServiceFeign.queryByUserId(commentsFeign.getCreatedBy());
        CommentsVO commentsVO = new CommentsVO();
        commentsVO.setNewsId(commentsFeign.getPnId());
        commentsVO.setCommentId(commentsFeign.getPhotoCommentId());
        commentsVO.setComment(commentsFeign.getComment());
        commentsVO.setCommentLike(commentsFeign.getCommentLike());
        commentsVO.setCreateTime(commentsFeign.getCreateTime());
        commentsVO.setHeadPic(user.getHeadPicture());
        commentsVO.setUserName(user.getLoginName());
        return commentsVO;
    }

    /**
     * 将图片资讯转换为前端vo展示
     * @param photoNewsFeign 图片资讯服务返回的图片资讯feign
     * @return 图片资讯VO
     */
    private PhotoNewsVO transformPhotoNewsFeign(PhotoNewsFeign photoNewsFeign){
        PhotoNewsVO photoNewsVO = new PhotoNewsVO();
        photoNewsVO.setId(photoNewsFeign.getId());
        photoNewsVO.setPnId(photoNewsFeign.getPnId());
        photoNewsVO.setPnNews(photoNewsFeign.getPnNews());
        photoNewsVO.setPnTitle(photoNewsFeign.getPnTitle());
        photoNewsVO.setUserId(photoNewsFeign.getUserId());
        photoNewsVO.setCreateTime(photoNewsFeign.getCreteTime());
        photoNewsVO.setClickNumber(photoNewsFeign.getClickNumber());
        ArrayList<PhotoNewsPictureFeign> pictureList = photoNewsFeign.getPictureList();
        ArrayList<String> pictures = new ArrayList<>();
        ArrayList<String> thumbs = new ArrayList<>();
        for (PhotoNewsPictureFeign picture:pictureList){
            String pictureId = picture.getPictureId();
            String pictureUrl = pictureServiceFeign.getPictureUrl(pictureId);
            String thumbUrl = pictureServiceFeign.getThumbnailUrl(pictureId);
            pictures.add(pictureUrl);
            thumbs.add(thumbUrl);
        }
        photoNewsVO.setPictureList(pictures);
        photoNewsVO.setThumbList(thumbs);
        return photoNewsVO;
    }
}
